package com.liu.QQ.client.service;

import com.liu.QQ.MessageType;
import com.liu.QQ.User;
import com.liu.QQ.Message;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;

/**
 * 用于完成登录注册验证等功能
 *
 * @author liuchengjie
 */
public class UserClientService {

    private User user = new User();
    private Socket socket;


    /**
     * 从服务器验证用户是否有效
     * @param userId
     * @param password
     * @return
     */
    public boolean checkUser(String userId,String password) throws IOException, ClassNotFoundException {
        boolean result = false;
        //构建user
        user.setUserId(userId);
        user.setPassword(password);
        //发送到服务端
        socket = new Socket("39.103.166.42", 9999);
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
        objectOutputStream.writeObject(user);

        //读取从服务端回送的message对象
        ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());
        Message message = (Message)objectInputStream.readObject();
        if (message.getMesType().equals(MessageType.MESSAGE_LOGIN_SUCCEED)) {
            //登录成功，起一个线程，持有socket与服务端交互-》创建一个类 ClientConnectThread
            ClientConnectThread clientConnectThread = new ClientConnectThread(socket);
            clientConnectThread.start();
            //为了客户端的扩展，将线程放入到集合中管理
            ManageClientConnectThread.addThreadToManage(userId,clientConnectThread);
            result = true;
        } else {
            //登录失败,没有启动和服务器通讯的线程，关闭socket
            socket.close();
        }
        return result;
    }

    /**
     * 获取在线用户列表
     */
    public void onlineFriendList() {
        //创建消息，设置类型
        Message message = new Message();
        message.setMesType(MessageType.MESSAGE_GET_ONLINE_FRIEND);
        message.setSender(user.getUserId());
        try {
            //发送给服务器
            ObjectOutputStream outputStream = new ObjectOutputStream(
                    ManageClientConnectThread.getConnectThread(user.getUserId()).getSocket().getOutputStream());
            outputStream.writeObject(message);


        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    /**
     * 退出当前系统
     * 当前客户端结束进程，服务端退出对应线程
     */
    public  void ExitClientSystem() {
        //构建消息体
        Message message = new Message();
        message.setMesType(MessageType.MESSAGE_CLIENT_EXIT);
        message.setSender(user.getUserId());
        //发送给服务器
        try {
            ObjectOutputStream outputStream = new ObjectOutputStream(
                    ManageClientConnectThread.getConnectThread(user.getUserId()).getSocket().getOutputStream());
            outputStream.writeObject(message);
            System.out.println(user.getUserId()+"退出了系统");
            //退出当前进程
            System.exit(0);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}


















